package com.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.system.mapper.AssessmentClassificationMapper;
import com.system.mapper.RolesMapper;
import com.system.pojo.AssessmentClassification;
import com.system.pojo.Roles;
import com.system.service.AssessmentClassificationService;
import com.system.util.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service("assessmentClassificationService")
public class AssessmentClassificationServiceImpl implements AssessmentClassificationService {
    @Autowired
    private AssessmentClassificationMapper assessmentClassificationMapper;
    JSONUtil jsonUtil = new JSONUtil();
    @Autowired
    private RolesMapper rolesMapper;
    @Override
    public List<AssessmentClassification> select(AssessmentClassification assessmentClassification) {
        return assessmentClassificationMapper.select(assessmentClassification);
    }

    @Override
    public String insert(AssessmentClassification assessmentClassification) {
        return jsonUtil.toJson("0", assessmentClassificationMapper.insert(assessmentClassification), "");
    }

    @Override
    public String update(AssessmentClassification assessmentClassification) {
        return jsonUtil.toJson("0", assessmentClassificationMapper.update(assessmentClassification), "");
    }

    @Override
    public String delete(String id) {
        return jsonUtil.toJson("0", assessmentClassificationMapper.delete(id), "");
    }

    @Override
    public String selectMenuTree(String roleid) {
        List<AssessmentClassification> list = assessmentClassificationMapper.select(null);
        JSONArray mains = new JSONArray();
        String[] menus = {};
        try {
            Roles roles = new Roles();
            roles.setId(roleid);
            String menu = rolesMapper.select(roles).get(0).getMenus();
            System.out.println(menu.toString());
            menus = menu.split(", ");
        } catch (Exception e) {
            System.out.println(e.toString());
            System.out.println("角色未拥有任何菜单权限，树形组件无任何选中节点！");
        }
        //循环取主分类
        for (int i = 0; i < list.size(); i++) {
            AssessmentClassification assessmentClassification = list.get(i);
            if (assessmentClassification.getSubid() == "0" || assessmentClassification.getSubid().equals("0")) {
                JSONObject main = new JSONObject();
                main.put("id", assessmentClassification.getId());
                main.put("title", assessmentClassification.getMainname());
                //循环取子分类
                JSONArray subs = new JSONArray();
                for (int j = 0; j < list.size(); j++) {
                    AssessmentClassification classification = list.get(j);
                    if (assessmentClassification.getId() == classification.getSubid()
                            || assessmentClassification.getId().equals(classification.getSubid())) {
                        JSONObject sub = new JSONObject();
                        sub.put("id", classification.getId());
                        sub.put("title", classification.getMainname());
                        //是否选中
                        for (int s = 0; s < menus.length; s++) {
                            if (menus[s] == classification.getId() || menus[s].equals(classification.getId())) {
                                sub.put("checked", true);
                            }
                        }
                        subs.add(sub);
                    }
                }
                main.put("children", subs);
                mains.add(main);
            }
        }
        return mains.toJSONString();
    }

    @Override
    public String selectAllMenus() {
        AssessmentClassification assessmentClassification = new AssessmentClassification();
        assessmentClassification.setSubid("0");
        return jsonUtil.toJson("0", assessmentClassificationMapper.select(assessmentClassification), "");
    }

    @Override
    public String turnOn(String id) {
        return jsonUtil.toJson("0", assessmentClassificationMapper.turnOn(id), "");
    }

    @Override
    public String turnOff(String id) {
        return jsonUtil.toJson("0", assessmentClassificationMapper.turnOff(id), "");
    }

    @Override
    public String selectSubMenus(String id) {
        return jsonUtil.toJson("0", assessmentClassificationMapper.selectSubMenus(id), "");
    }

    public RolesMapper getRolesMapper() {
        return rolesMapper;
    }

    public void setRolesMapper(RolesMapper rolesMapper) {
        this.rolesMapper = rolesMapper;
    }

    public AssessmentClassificationMapper getAssessmentClassificationMapper() {
        return assessmentClassificationMapper;
    }

    public void setAssessmentClassificationMapper(AssessmentClassificationMapper assessmentClassificationMapper) {
        this.assessmentClassificationMapper = assessmentClassificationMapper;
    }
}
